package com.bjb.vr.remote.ws.websocket.websocket;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;

import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.websocket.constants.WebSocketConstants;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class WebSocketMsgHandler {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketMsgHandler.class);

//    public static boolean isNotificationMsg(String msg){
//        if(StringUtils.isEmpty(msg)){
//            logger.error("消息为空");
//            return false;
//        }
//        try {
//            JSONObject jsonObject = JSONObject.parseObject(msg);
//            RabbitWebSocketMsg rabbitWebSocketMsg = JSONObject.toJavaObject(jsonObject, RabbitWebSocketMsg.class);
//
//            String socketMsg = rabbitWebSocketMsg.getMsg();
//            JSONObject sokcetJsonObject = JSONObject.parseObject(socketMsg);
//            Message _msg = JSONObject.toJavaObject(sokcetJsonObject, Message.class);
//
//            return WebSocketConstants.WEBSOCKET_COMMAND_ACTION_NOTIFICATION.equals(_msg.getAction());
//        }catch (Exception ex){
//            ex.printStackTrace();
//            logger.error("解析消息出错,消息内容 " + msg,ex);
//            return false;
//        }
//
//    }

//    public static void  processNotification(String msg){
//        processClientMsg(msg);
//    }

    //判断消息是否是管理端消息
    public static  boolean isAdminMsg(String msg){
        if(StringUtils.isEmpty(msg)){
            logger.error("消息为空");
            return false;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(msg);
            RabbitWebSocketMsg rabbitWebSocketMsg = JSONObject.toJavaObject(jsonObject, RabbitWebSocketMsg.class);
            return "1".equals(rabbitWebSocketMsg.getIsAdmin());
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("解析消息出错,消息内容 " + msg,ex);
            return false;
        }
    }


    /**
     * 发送给指定眼镜端
     * @param
     */
    public static void sendSingleClient(RabbitWebSocketMsg rabbitWebSocketMsg,Message message){
        Map<String, WebSocketServer> room = WebSocketServer.classMap.get(rabbitWebSocketMsg.getGroupCode());
        if (room == null) {
            return;
        }

        for (Iterator<String> iterator = room.keySet().iterator(); iterator
                .hasNext(); ) {
            String dev = iterator.next();
            WebSocketServer member = room.get(dev);
            try {
                if (!member.isAdmin() && member.getState() == WebSocketConstants.WEBSOCKET_CLIENT_STATE_OPENED
                        && member.getDeviceId().equals(message.getCommand().getUdid())) {
                    logger.info("发送单个消息 " + rabbitWebSocketMsg.getMsg() + " 接受方 " + member.getDeviceId());
                    member.sendMsg(rabbitWebSocketMsg.getMsg());
                }
            } catch (Exception ex) {
                logger.error("发送消息失败," + " [设备ID]" + member.getDeviceId() + ",[房间号]" + member.getGroupCode());
            }
        }


    }

    /**
     * 发送给所有眼镜端
     * @param
     */
    public static void sendAllClient(RabbitWebSocketMsg rabbitWebSocketMsg){

        Map<String, WebSocketServer> room = WebSocketServer.classMap.get(rabbitWebSocketMsg.getGroupCode());
        if (room == null) {
            return;
        }

        room.entrySet().parallelStream().forEach(dev->{
            WebSocketServer member = dev.getValue();
            try {
                if (!member.isAdmin() && member.getState() == WebSocketConstants.WEBSOCKET_CLIENT_STATE_OPENED) {
                    logger.info("发送群发消息 " + rabbitWebSocketMsg.getMsg() + " 接受方 " + member.getDeviceId());
                    member.sendMsg(rabbitWebSocketMsg.getMsg());
                }
            } catch (Exception ex) {
                logger.error("发送消息失败," + " [设备ID]" + member.getDeviceId() + ",[房间号]" + member.getGroupCode());
            }
        });
//        for (Iterator<String> iterator = room.keySet().iterator(); iterator
//                .hasNext(); ) {
//            String dev = iterator.next();
//            WebSocketServer member = room.get(dev);
//            try {
//                if (!member.isAdmin() && member.getState() == WebSocketConstants.WEBSOCKET_CLIENT_STATE_OPENED)
//                    member.sendMsg(rabbitWebSocketMsg.getMsg());
//            } catch (Exception ex) {
//                logger.error("发送消息失败," + " [设备ID]" + member.getDeviceId() + ",[房间号]" + member.getGroupCode());
//            }
//        }


    }

    /**
     * 发送给管理端
     */
    public static void sendAdmin(RabbitWebSocketMsg rabbitWebSocketMsg){


            Map<String, WebSocketServer> room = WebSocketServer.classMap.get(rabbitWebSocketMsg.getGroupCode());
            if(room == null){
                return ;
            }

            for(Iterator<String> iterator = room.keySet().iterator(); iterator
                    .hasNext();)
            {
                String dev = iterator.next();
                WebSocketServer member = room.get(dev);
                try {
                    if (member.isAdmin() && member.getState() == WebSocketConstants.WEBSOCKET_CLIENT_STATE_OPENED) {
                        member.sendMsg(rabbitWebSocketMsg.getMsg());
                    }
                }catch (Exception ex) {
                    logger.error("发送消息失败," + " [设备ID]" + member.getDeviceId() + ",[房间号]" + member.getGroupCode());
                }
            }


    }

    public static Set<String> getWebsocketConnectDevSet(String groupCode){
        Set<String>  devSet = new HashSet<>();
        Map<String,WebSocketServer> groupMap =  WebSocketServer.classMap.get(groupCode);
        if(groupMap!=null && groupMap.size()>0){
            devSet.addAll(groupMap.keySet());
        }
        return devSet;
    }

}
